/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.tag;

import edu.fpt.dal.ApartmentDAL;
import edu.fpt.dal.UsersDAL;
import edu.fpt.hib.Apartment;
import edu.fpt.hib.Users;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Admin
 */
public class UserListTag extends SimpleTagSupport {

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = getJspContext().getOut();

        try {
            // TODO: insert code to write html before writing the body content.
            // e.g.:
            
            // out.println("<strong>" + attribute_1 + "</strong>");
            // out.println("    <blockquote>");
            out.println("<table id=\"mytable\">");
            out.println("<tr>");
            out.println("<th scope=\"col\" class=\"nobg\">Customer Name</th>"
                    + "<th scope=\"col\">Username</th>"
                    + "<th scope=\"col\">User role</th>"
                    + "<th scope=\"col\">Create Date</th>"
                    + "<th scope=\"col\">Living in</th>"
                    + "<th scope=\"col\">Edit</th>"
                    + "<th scope=\"col\">Delete</th>"
                    + "</tr>");
            ArrayList< Users> list = UsersDAL.listAll();
            for (Users user : list) {
                Apartment apart = ApartmentDAL.getApartmentByUserHQL(user.getUserId());
                out.println("<tr> <th class=\"spec\">" + user.getUserFullName() + "</th>");
                out.println("<td class=\"alt\"><a href=\"detailuser.jsp?userid=" + user.getUserId() + "\">" + user.getUserId() + "</a></td>");
                int role = user.getUserRole();
                out.println("<td class=\"alt\">");
                if (role == 1) {
                    out.println("Administrator");
                } else {
                    out.println("Normal User");
                }
                out.println("</td> <td class=\"alt\">" + user.getUserCreateDate() + "</td>");
                if (apart != null) {
                    out.println("<td class=\"alt\">" + apart.getApartId() + "</td>");
                } else {
                    out.println("<td class=\"alt\"> None </td>");

                }
                out.println("<td class=\"alt\"><form method=\"get\" action=\"edituser.jsp\"><input type=\"hidden\" name=\"userid\" value=\"" + user.getUserId() + "\">"
                        + "<input class=\"searchsubmit\" type=\"submit\" value=\"Edit\">"
                        + "</form></td>"
                        + "<td class =\"alt\" ><form method=\"get\" action=\"deleteuser.jsp\">"
                        + "<input type=\"hidden\" name=\"userid\" value=\"" + user.getUserId() + "\">"
                        + "<input class =\"searchsubmit\" type=\"submit\" value=\"Delete\">"
                        + "</form></td>"
                        + "</tr>");

            }out.println("</table>");
            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in UserListTag tag", ex);
        }
    }
}
